﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoNiuOA.Models;
using PagedList;

namespace TaoNiuOA.Web.Controllers
{
    public class CheckingController : Controller
    {
        private DbEntity db = new DbEntity();

        int pageSize = 10;

        [IsAdmin]
        public ViewResult Index(int? page)
        {
            ViewData["actionName"] = "Index";
            var index = (page ?? 1) - 1;
            var user = Session["users"] as TaoNiuOA.Models.User;

            var today = DateTime.Now.ToString("yyyy-MM-dd");
            var list = new List<Checking>();

            var mod = Request.QueryString["mod"];
            ViewData["mod"] = mod;

            //根据条件查询
            if (!string.IsNullOrEmpty(mod) && mod.IndexOf("-") != -1)
            {
                list = (from n in db.Checkings where n.Date.Contains(mod) select n).ToList();
            }
            else
            {
                switch (mod)
                {
                    //所有
                    case "all":
                        list = (from n in db.Checkings where n.UserID == user.UserID select n).ToList();
                        break;
                    //昨天 
                    case "yesterday":
                        today = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
                        list = (from n in db.Checkings where n.Date.Contains(today) select n).ToList();
                        break;
                    //本月
                    case "thismonth":
                        today = DateTime.Now.ToString("yyyy-MM");
                        list = (from n in db.Checkings where n.Date.Contains(today) select n).ToList();
                        break;
                    //上月 
                    case "lastmonth":
                        today = DateTime.Now.AddMonths(-1).ToString("yyyy-MM");
                        list = (from n in db.Checkings where n.Date.Contains(today) select n).ToList();
                        break;
                    //默认,今天
                    default:
                        list = (from n in db.Checkings where n.Date.Contains(today) select n).ToList();
                        break;
                }
            }
            return View(list.ToPagedList(index, pageSize));
        }

        //public ViewResult Details(int id)
        //{
        //    Checking checking = db.Checkings.Find(id);
        //    return View(checking);
        //}

        //public ActionResult Create()
        //{
        //    return View();
        //}

        //[HttpPost]
        //public ActionResult Create(Checking checking)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Checkings.Add(checking);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }

        //    return View(checking);
        //}

        //public ActionResult Edit(int id)
        //{
        //    Checking checking = db.Checkings.Find(id);
        //    return View(checking);
        //}


        //[HttpPost]
        //public ActionResult Edit(Checking checking)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Entry(checking).State = EntityState.Modified;
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    return View(checking);
        //}

        //签到签退

        [IsLogon]
        [HttpPost]
        public ActionResult Sign()
        {
            try
            {
                var mod = Request.QueryString["mod"];
                var user = Session["users"] as TaoNiuOA.Models.User;
                var today = DateTime.Now.ToString("yyyy-MM-dd");
                var check = db.Checkings.SingleOrDefault(n => n.UserID == user.UserID && n.Date == today);

                //如记录不存在则新建
                if (check == null)
                {
                    check = new Checking();
                    check.UserID = user.UserID;
                    check.Date = today;
                    db.Checkings.Add(check);
                    db.SaveChanges();
                }

                db.Entry(check).State = EntityState.Modified;
                //签到
                if (mod == "signIn")
                {
                    check.OnWork = DateTime.Now.ToString("HH:mm");

                    var workDay = db.Workdays.SingleOrDefault(n => n.Date == today);
                    var start = Convert.ToDateTime(workDay.OnWork);
                    var now = Convert.ToDateTime(check.OnWork);
                    var minutes = (now - start).TotalMinutes;
                    if (minutes > 30)
                    {
                        check.Remark += "迟到" + minutes + "分钟.\n" ;
                    }
                    db.SaveChanges();
                }
                //签退
                else
                {
                    check.OffWork = DateTime.Now.ToString("HH:mm");
                    var workDay = db.Workdays.SingleOrDefault(n => n.Date == today);
                    var start = Convert.ToDateTime(workDay.OffWork);
                    var now = Convert.ToDateTime(check.OffWork);
                    var minutes = (now - start).TotalMinutes;
                    if (minutes < 30)
                    {
                        check.Remark += "早退" + minutes + "分钟.\n";
                    }
                    db.SaveChanges();
                }
                return Json("ok");
            }
            catch
            {
                return Json("error");
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}